package com.duomai.myapplication.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.recyclerview.widget.RecyclerView;

import com.duomai.myapplication.R;
import com.duomai.myapplication.adapter.ComplaintAdapter;
import com.example.admin.SQLite.Complaint;
import com.example.admin.SQLite.SqLiteOpenHelper;
import com.example.admin.SQLite.User;
import com.example.admin.SQLite.YiJian;
import com.example.admin.SQLite.dao.YiJianDao;
import com.example.admin.base.BaseActivity;
import com.orhanobut.hawk.Hawk;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//投诉建议
public class ComplaintActivity extends BaseActivity {
    RecyclerView recyclerView;
    ComplaintAdapter mAdapter;

    YiJianDao yiJianDao;

    @Override
    public void setContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_complaint);
        yiJianDao = new YiJianDao(this);
        recyclerView = findViewById(R.id.recyclerView);
        mAdapter = new ComplaintAdapter();
        recyclerView.setAdapter(mAdapter);
        findViewById(R.id.tvSure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ComplaintActivity.this);
                builder.setTitle("请输入投诉建议");
                final EditText input = new EditText(ComplaintActivity.this);
                input.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        String str = s.toString();
                        if (str.length() > 20) {
                            input.setText(str.substring(0, 20));
                        }

                    }
                });
                builder.setView(input);
                builder.setPositiveButton("发布", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Date currentTime = new Date();
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String time = format.format(currentTime);
                        String text = input.getText().toString();
                        User user = Hawk.get("user");
                        YiJian bean = new YiJian();
                        bean.yijianContent = text;
                        bean.yijianState = "待处理";
                        bean.yijianTime = time;
                        bean.uid = user.id;
                        yiJianDao.addData(bean);
                        getdata();
                    }
                });

// 显示对话框

                builder.show();
            }
        });

    }

    private void getdata() {
        User user = Hawk.get("user");
        List<YiJian> list = yiJianDao.getComPlanc(user.id + "");
        mAdapter.setNewData(list);
    }

    @Override
    protected void onStart() {
        super.onStart();
        getdata();
    }

    @Override
    public void setInitView() {

    }
}